package com.bw.controller;

import com.bw.entity.*;
import com.bw.mapper.HouseMapper;
import com.bw.mapper.RegionMapper;
import com.bw.service.HouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.spring.kun.common.TokenUtils;
import org.spring.kun.common.UploadPicUtils;
import org.spring.kun.common.ZzyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Controller
public class HouseController {

    @Autowired
    RegionMapper regionMapper;
    @Autowired
    HouseMapper houseMapper;
    @Autowired
    HouseService houseService;

    @RequestMapping("/list.do")
    public String list(HouseQuery query, Integer pageNum, HttpServletRequest request, Model model) {
        // 展示绑定之后的昵称
        User userFromRequest = TokenUtils.getLoginUserFromRequest(request, User.class);
        model.addAttribute("nickName", userFromRequest.getNickName());

        //渲染下拉框  （省）
        List<Dict> provinceList = regionMapper.listProvince();
        model.addAttribute("provinceList", provinceList);

        // 执行分页
        PageHelper.startPage(pageNum == null ? 1 : pageNum, 30);

        List<House> list = houseMapper.selectByPage(query);
        PageInfo<House> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);

        // 条件回显
        model.addAttribute("query", query);
        // 回显城市
        if (query.getProvinceId() != null) {
            List<Dict> cityList = regionMapper.selectCity(query.getProvinceId());
            model.addAttribute("cityList", cityList);
        }
        return "list";
    }


    @RequestMapping("/selectCity.do")
    @ResponseBody
    public List<Dict> selectCity(Integer provinceId) {
        return regionMapper.selectCity(provinceId);
    }


    @RequestMapping("/addPage.do")
    public String addPage(Model model) {
        // 省份下拉框
        //渲染下拉框  （省）
        List<Dict> provinceList = regionMapper.listProvince();
        model.addAttribute("provinceList", provinceList);

        return "add";
    }



    @RequestMapping("/add.do")
    public String add(@Valid HouseAdd houseAdd ,BindingResult result,Model model,HttpServletRequest request) {

        if (result.hasErrors()){
          model.addAttribute("error",result.getAllErrors().get(0).getDefaultMessage());
            //渲染下拉框  （省）
            List<Dict> provinceList = regionMapper.listProvince();
            model.addAttribute("provinceList", provinceList);
          return "add";
        }
        // 开始解析图片并保存到webapp下

        String url = UploadPicUtils.upload(houseAdd.getPicFile(), request);
        houseAdd.setPicUrl(url);

        houseService.add(houseAdd);

        // 给管理员发短信  在什么时间添加了楼盘信息
        boolean sendSms = ZzyUtils.sendSms("17611221735", System.currentTimeMillis() + "成功添加了楼盘", 5);
        //短信发送成功，修改楼盘信息短信发送的状态
        if (sendSms){
            houseMapper.updateSms(houseAdd.getId());
        }

        return "redirect:list.do";
    }
}
